package com.maybach.core.ao.impl;

import com.maybach.common.model.LoanOrderDO;
import com.maybach.common.model.LogisticsCompanyDO;
import com.maybach.common.model.ShippingOrderDO;
import com.maybach.core.ao.LoanOrderAO;
import com.maybach.core.ao.ShippingCarsAO;
import com.maybach.core.ao.UserMsgAO;
import com.maybach.core.enums.NumEnum;
import com.maybach.core.form.shippingCar.DeliveryCarForm;
import com.maybach.core.form.shippingCar.LogisticsInfoForm;
import com.maybach.core.generator.NumGenerator;
import com.maybach.core.manager.LoanOrderManager;
import com.maybach.core.manager.LogisticsCompanyManager;
import com.maybach.core.manager.ShippingOrderManager;
import com.maybach.util.AssertUtil;
import com.maybach.util.CalendarUtil;
import com.maybach.util.ImgUrlUtil;
import com.maybach.util.PriceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by weishuai on 16/3/2.
 */
@Component
public class ShippingCarAOImpl implements ShippingCarsAO {
    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    ShippingOrderManager shippingOrderManager;

    @Autowired
    NumGenerator numGenerator;
    @Autowired
    UserMsgAO userMsgAO;

    @Autowired
    LoanOrderManager loanOrderManager;
    @Autowired
    LogisticsCompanyManager logisticsCompanyManager;

    @Override
    public void updateDeliveryCar(DeliveryCarForm d) {
        ShippingOrderDO s = getShippingOrderDO(d);
        shippingOrderManager.updateByPrimaryKeySelective(s);
    }

    private ShippingOrderDO getShippingOrderDO(DeliveryCarForm d) {
        ShippingOrderDO s = new ShippingOrderDO();
        s.setOuterOrderId(d.getOuterOrderId());
        s.setShippingPlace(d.getShippingPlace());
        s.setShippingContact(d.getShippingContact());
        s.setShippingConfirmImgs(ImgUrlUtil.mergeList(d.getShippingConfirmImgs()));
        s.setShippingPhone(d.getShippingPhone());
        s.setActualRecerveTime(CalendarUtil.toDate(d.getActualRecerveTime(), CalendarUtil.TIME_PATTERN));
        s.setId(d.getId());
        s.setShippingEntrustImgs(ImgUrlUtil.mergeList(d.getShippingEntrustImgs()));
        s.setShippingTime(CalendarUtil.toDate(d.getShippingTime(), CalendarUtil.TIME_PATTERN));
        return s;
    }

    @Override
    public Long addDeliveryCar(DeliveryCarForm deliveryCarForm) {
        ShippingOrderDO s = getShippingOrderDO(deliveryCarForm);
        LoanOrderDO loanOrderDO = loanOrderManager.selectByOutOrderId(deliveryCarForm.getOuterOrderId());
        s.setOrderNo(numGenerator.generate(NumEnum.shippingOrder));
        s.setStatus(2);//已接车
        shippingOrderManager.insertSelective(s);
        return s.getId();
    }

    @Override
    public DeliveryCarForm getById(Long id) {
        DeliveryCarForm d = new DeliveryCarForm();
        ShippingOrderDO shippingOrderDO = shippingOrderManager.selectByPrimaryKey(id);
        if (shippingOrderDO != null) {
            d.setActualRecerveTime(CalendarUtil.toString(shippingOrderDO.getActualRecerveTime(), CalendarUtil.TIME_PATTERN));
            d.setId(id);
            d.setShippingConfirmImgs(ImgUrlUtil.parseList(shippingOrderDO.getShippingConfirmImgs()));
            d.setShippingEntrustImgs(ImgUrlUtil.parseList(shippingOrderDO.getShippingEntrustImgs()));
            d.setShippingContact(shippingOrderDO.getShippingContact());
            d.setShippingPhone(shippingOrderDO.getShippingPhone());
            d.setShippingPlace(shippingOrderDO.getShippingPlace());
            d.setShippingTime(CalendarUtil.toString(shippingOrderDO.getShippingTime(), CalendarUtil.TIME_PATTERN));
        }
        return d;
    }

    @Override
    public DeliveryCarForm getByOutOrderId(Long outOrderId) {
        DeliveryCarForm d = new DeliveryCarForm();
        ShippingOrderDO shippingOrderDO = shippingOrderManager.selectByOutOrderId(outOrderId);
        if (shippingOrderDO != null) {
            d.setActualRecerveTime(CalendarUtil.toString(shippingOrderDO.getActualRecerveTime(), CalendarUtil.TIME_PATTERN));
            d.setId(shippingOrderDO.getId());
            d.setOuterOrderId(shippingOrderDO.getOuterOrderId());
            d.setShippingConfirmImgs(ImgUrlUtil.parseList(shippingOrderDO.getShippingConfirmImgs()));
            d.setShippingEntrustImgs(ImgUrlUtil.parseList(shippingOrderDO.getShippingEntrustImgs()));
            d.setShippingContact(shippingOrderDO.getShippingContact());
            d.setShippingPhone(shippingOrderDO.getShippingPhone());
            d.setShippingPlace(shippingOrderDO.getShippingPlace());
            d.setShippingTime(CalendarUtil.toString(shippingOrderDO.getShippingTime(), CalendarUtil.TIME_PATTERN));
        }
        return d;
    }


    @Override
    public LogisticsInfoForm getLogisticsInfoByOutOrderId(Long outOrderId) {
        ShippingOrderDO s = shippingOrderManager.selectByOutOrderId(outOrderId);
        LogisticsInfoForm l = new LogisticsInfoForm();
        if (s != null) {
            l.setId(s.getId());
            l.setOuterOrderId(s.getOuterOrderId());
            if(s.getActualPostTime()!=null){
                l.setActualPostTime(CalendarUtil.toString(s.getActualPostTime(), CalendarUtil.TIME_PATTERN));
            }
            l.setAmount(s.getAmount() + "");
            l.setCarUnique(s.getCarUnique());
            l.setDriverName(s.getDriverName());
            l.setDriverPhone(s.getDriverPhone());
            l.setFromCity(s.getFromcity());
            l.setToCity(s.getTocity());
            l.setAmountProfImgs(ImgUrlUtil.parseList(s.getAmountProfImgs()));
            l.setCheckCarImgs(ImgUrlUtil.parseList(s.getCheckCarImgs()));
            l.setGpsNum(s.getGpsNum());
            l.setLicenseNumber(s.getLicenseNumber());
            l.setLogisticsNo(s.getLogisticsNo());
            l.setLogisticsCompany(s.getLogisticsCompany());
            l.setLogisticsCompanyId(s.getLogisticsCompanyId());
        }
        return l;
    }

    @Override
    public void updateLogisticsInfo(LogisticsInfoForm logisticsInfoForm) {
        ShippingOrderDO shippingOrderDO = getShippingOrderDO(logisticsInfoForm);
        shippingOrderManager.updateByPrimaryKeySelective(shippingOrderDO);

    }

    private ShippingOrderDO getShippingOrderDO(LogisticsInfoForm logisticsInfoForm) {
        ShippingOrderDO shippingOrderDO = new ShippingOrderDO();
        shippingOrderDO.setId(logisticsInfoForm.getId());
        shippingOrderDO.setAmount(PriceUtil.convertPriceYuanWithComma(logisticsInfoForm.getAmount()));
        shippingOrderDO.setActualPostTime(CalendarUtil.toDate(logisticsInfoForm.getActualPostTime(), CalendarUtil.TIME_PATTERN));
        shippingOrderDO.setLogisticsCompanyId(logisticsInfoForm.getLogisticsCompanyId());
        LogisticsCompanyDO logisticsCompanyDO = logisticsCompanyManager.selectByPrimaryKey(logisticsInfoForm.getLogisticsCompanyId());
        AssertUtil.notNull(logisticsCompanyDO, "logisticsCompanyDO");
        shippingOrderDO.setLogisticsCompany(logisticsCompanyDO.getCompanyName());
        shippingOrderDO.setAmountProfImgs(ImgUrlUtil.mergeList(logisticsInfoForm.getAmountProfImgs()));
        shippingOrderDO.setLicenseNumber(logisticsInfoForm.getLicenseNumber());
        shippingOrderDO.setLogisticsNo(logisticsInfoForm.getLogisticsNo());
        shippingOrderDO.setCarUnique(logisticsInfoForm.getCarUnique());
        shippingOrderDO.setCheckCarImgs(ImgUrlUtil.mergeList(logisticsInfoForm.getCheckCarImgs()));
        shippingOrderDO.setDriverPhone(logisticsInfoForm.getDriverPhone());
        shippingOrderDO.setDriverName(logisticsInfoForm.getDriverName());
        shippingOrderDO.setFromcity(logisticsInfoForm.getFromCity());
        shippingOrderDO.setTocity(logisticsInfoForm.getToCity());
        shippingOrderDO.setGpsNum(logisticsInfoForm.getGpsNum());
        shippingOrderDO.setOuterOrderId(logisticsInfoForm.getOuterOrderId());
        return shippingOrderDO;
    }

    @Override
    public Long addLogisticsInfo(LogisticsInfoForm logisticsInfoForm) {
        ShippingOrderDO shippingOrderDO = getShippingOrderDO(logisticsInfoForm);
        shippingOrderDO.setStatus(1);

        Long id= shippingOrderManager.insertSelective(shippingOrderDO);
        try {
            LoanOrderDO loanOrderDO=loanOrderManager.selectByPrimaryKey(logisticsInfoForm.getOuterOrderId());
            userMsgAO.shipping(loanOrderDO, shippingOrderDO);
        } catch (Exception e) {
            logger.error("error in addLogisticsInfo,logisticsInfoForm,",logisticsInfoForm.toString());
        }
        return id;
    }
}
